package com.wy.base;

import java.lang.reflect.Field;

import javax.persistence.Table;

import com.wy.result.ResultException;
import com.wy.utils.StrUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * @apiNote 所有分页查询的基础类,T为需要查询的分页数据的主实体类,不可为object
 * @author ParadiseWY
 * @date 2019年7月30日 上午11:57:20
 */
@Getter
@Setter
public abstract class AbstractPage extends AbstractPager {

	private static final long serialVersionUID = 1L;

	public CharSequence getColumns(Class<?> clazz) {
		Field[] fields = clazz.getDeclaredFields();
		if (fields.length == 0) {
			throw new ResultException(clazz.getSimpleName() + "实体类中无字段");
		}
		StringBuilder sb = new StringBuilder();
		for (Field field : fields) {
			sb.append(StrUtils.hump2Snake(field.getName()) + ",");
		}
		return sb.deleteCharAt(sb.length() - 1).toString();
	}

	public CharSequence getTables(Class<?> clazz) {
		if (clazz.isAnnotationPresent(Table.class)) {
			return clazz.getAnnotation(Table.class).name();
		} else {
			return clazz.getSimpleName();
		}
	}

	public void getWheres() {
	}

	public String getGroupBy() {
		return null;
	}

	public CharSequence getOrderBy() {
		return null;
	}
}